<template>
	<view>
		<view class="add-coupon">
			<text @click="add">新增优惠券</text>
			<button @click="add" class="add">+新增</button>

		</view>
		<view class="button">
			<button :class="!isGoing?'btn':''" @click="changeGoing(0)">进行中</button>
			<button :class="isGoing?'btn':''" @click="changeGoing(1)">已结束</button>
		</view>
		<view v-if="couponList.length === 0" style="margin-top: 60rpx;margin-left: 280rpx;font-size: 40rpx;">
			暂无优惠券
		</view>
		<view class="couponList" :class="!isGoing?'bgcolor':''" v-for="(item,index) in couponList" :key="index" @click="showRemainingCoupons">
			<view class="coupon-left" v-if="item.type==='满减券'">
				<text>满{{item.triggerMoney}}减</text>
				<text>{{item.preferentialMoney}}</text>
			</view>
			<view class="coupon-left" v-if="item.type==='折扣券'">
				<text></text>
				<text style="margin-left: 20rpx;">{{item.discount*10}}折</text>
			</view>
			<view class="coupon-right">
				<view>优惠券名称:{{item.name}}</view>
				<view>有效期:{{item.start}}-{{item.end}}</view>
				<view>每人限领{{item.size}}张</view>
			</view>
		</view>

		<!-- 对话框 -->
		<view class="dialog" v-if="showDialog">
			<view class="dialog-content" @click="hideDialog">
				<text>剩余优惠券数量: {{ remainingCoupons }}张</text>
				<!-- <button @click="hideDialog" >关闭</button> -->
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isGoing: true,
				showDialog: false, // 控制对话框显示  
				remainingCoupons: 5, // 假设剩余5张优惠券  
				token: getApp().globalData.authorization,
				couponList: [],
				status: 0,
			}
		}, 
		onLoad() {
			this.getCouponsByShopId(0)
		},
		methods: {
			changeGoing(sta) {
				this.isGoing = !this.isGoing
				this.getCouponsByShopId(sta)
			},
			add() {
				uni.redirectTo({
					url: '/pages/coupon/coupondetail/coupondetail'
				})
			},
			showRemainingCoupons() {
				this.showDialog = true; // 显示对话框  
			},
			hideDialog() {
				this.showDialog = false; // 隐藏对话框  
			},
			getCouponsByShopId(sta){
				uni.request({
					url:'http://localhost:8088/business/coupon/list',
					method:'GET',
					header:{
						authorization:this.token
					},
					data:{
						mId: getApp().globalData.userShop.id,
						status: sta
					},
					success:(resp)=> {
						this.couponList = resp.data.rows
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}

	.add-coupon {
		margin: 24rpx 10rpx 10rpx 10rpx;
		width: 725rpx;
		height: 166rpx;
		border: 1rpx solid rgba(187, 187, 187, 1);
		position: relative;
	}

	.add-coupon text {
		position: absolute;
		top: 56rpx;
		left: 50rpx;
		line-height: 50rpx;
		color: rgb(13, 122, 247);
		font-size: 36rpx;
		text-align: left;
		vertical-align: middle;
	}

	/* 新增按钮 */
	.add {
		left: 210rpx;
		top: 50rpx;
		width: 160rpx;
		height: 68rpx;
		border-radius: 8rpx;
		background-color: rgba(255, 255, 255, 1);
		color: rgba(16, 16, 16, 1);
		font-size: 28rpx;
		text-align: center;
		font-family: Roboto;
		border: 1px solid rgba(187, 187, 187, 1);
	}

	.add button {
		width: 117px;
		height: 34px;
		border-radius: 4px;
		font-size: 14px;
		margin-left: 122rpx;
		background-color: rgb(13, 122, 247);
		color: #fff;
		display: inline-block;
		vertical-align: middle;
	}

	.button {
		margin-top: 72rpx;
		display: flex;

		.btn {
			background-color: #89B0EC;
		}
	}

	.button button {
		width: 94px;
		height: 34px;
		line-height: 34px;
		background-color: #0D7AF7;
		color: #fff;
		border-radius: 25rpx;
	}

	.couponList {
		background-color: #0D7AF7;
		height: 202rpx;
		margin: 46rpx 34rpx;
		border-radius: 20rpx;
		display: flex;

		.coupon-left {
			height: 174rpx;
			width: 234rpx;
			margin-top: 22rpx;
			text-align: center;
		}

		.coupon-left>text:nth-child(1) {
			line-height: 20px;
			color: rgba(255, 255, 255, 1);
			font-size: 14px;
			font-family: PingFangSC-regular;
		}

		.coupon-left>text:nth-child(2) {
			color: rgba(255, 255, 255, 1);
			font-size: 48px;
			display: block;
			line-height: 67px;
			font-family: PingFangSC-regular;
		}

		.coupon-right {
			height: 140rpx;
			margin-top: 36rpx;
			border-left: 2rpx solid #fff;
		}

		.coupon-right view {
			margin-left: 58rpx;
			color: #cadbf6;
			font-size: 12px;
			text-align: left;
			font-family: PingFangSC-regular;
		}

		.coupon-right view {
			color: #cae2fd !important;
		}
	}

	.bgcolor {
		background-color: #89B0EC;
	}
	
	/* 样式部分 */  
	.dialog {  
	  position: fixed;  
	  top: 42%;  
	  left: 50%;  
	  transform: translate(-50%, -50%);  
	  background-color: white;  
	  padding: 20px;  
	  border-radius: 10px;  
	  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);  
	  z-index: 1000; /* 确保对话框在顶层 */  
	}  
	  
	.dialog-content button {  
	  /* 按钮样式 */  
	} 
</style>